package com.itzz.spring_zhoneHeng.service.Impl;

import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.MessagesDao;
import com.itzz.spring_zhoneHeng.pojo.Messages;
import com.itzz.spring_zhoneHeng.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class MessagesServiceImpl implements MessagesService {

    @Autowired
    MessagesDao messagesDao;

    @Override
    @Transactional
    //根据前端传输的messages  消息  添加到 messages表中
    //根据传输的用户数组  ids   和messages表中的id 添加进u_ms表中
    public RestResponse addMessages(Messages messages,Integer[] uids){
        Date date = new Date();
        messages.setId(0);
        messages.setDate(date);
        //根据前端传输的messages  消息  添加到 messages表中
        int i = messagesDao.addMessages(messages);
        //根据传输的用户数组  ids   和messages表中的id 添加进u_ms表中
        int i1 = messagesDao.addU_MS(uids, messages.getId());
        if(i1>0){
            return RestResponse.ok();
        }
        return RestResponse.fail(0,"添加失败");

    }


    @Override
    @Transactional
    //根据前端传出来的messids  删除u_ms表中的数据
    public RestResponse delByMessId(Integer[] messids){
        //根据messids 删除表中的数据
        messagesDao.deleteByMessId(messids);
        int i = messagesDao.deleteByMessIds(messids);
        //根据

        if(i>0){
            return RestResponse.ok();
        }
        return RestResponse.fail(0,"删除失败");
    }




}
